package jsj.open.lib.piccrop

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.ImageView
import androidx.core.graphics.forEach

class ImageCrop(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    ImageView(context, attrs, defStyleAttr) {

    constructor(context: Context?):this(context,null,0)
    constructor(context: Context?,attrs: AttributeSet?) : this(context,attrs,0)

//    private var imageBitmap:Bitmap? = null
    private val paint = Paint()
    private var pointsRect: PointsRect = PointsRect()
    init {
        scaleType = ScaleType.FIT_XY
    }

//    override fun setImageBitmap(bm: Bitmap?) {
//        super.setImageBitmap(bm)
//        imageBitmap = bm
//    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    @SuppressLint("DrawAllocation")
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val w = MeasureSpec.getSize(widthMeasureSpec)
        val h = MeasureSpec.getSize(heightMeasureSpec)
        val r = w
        val b = r*9f/16
        var rectCrop = Rect(0,0,r,b.toInt())
//        imageBitmap?.let {
//            if (1f*w/h!=1f*it.width/it.height){
//                val realh = w.toFloat()*it.height.toFloat()/it.width.toFloat()
//                Log.e("TAG", "onMeasure: $w ,$h ,bitmap:${it.width},${it.height} ,real:$realh", )
//                setMeasuredDimension(w,realh.toInt())
//                rectCrop = Rect(w/4,0,w/2,realh.toInt())
//
//            }
//        }
//        val rh = rectCrop.bottom - rectCrop.top
//        val rw = 16f/9f*rh
//        rectCrop.left = (w/2-rw/2).toInt()
//        rectCrop.right = (w/2+rw/2).toInt()
        pointsRect.set(rectCrop)
        pointsRect.setMax(measuredWidth,measuredHeight)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        canvas!!.save()
        super.onDraw(canvas)
        val re = Region()
        re.set(0,0,width,height)
        re.op(pointsRect.bounds,Region.Op.XOR)
        paint.style = Paint.Style.FILL
        paint.color = Color.parseColor("#A0000000")
        re.forEach {
//            Log.e(TAG, "onDraw: rect = ${it}", )
            canvas.drawRect(it,paint)
        }

        paint.color = Color.parseColor("#2DCCD3")
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 3f
        canvas.drawRect(pointsRect.bounds,paint)
        pointsRect.pointsAll().forEach {
            paint.style = Paint.Style.FILL
            paint.color = Color.WHITE
            canvas.drawRect(it.rect,paint)
            paint.style = Paint.Style.STROKE
            paint.color = Color.parseColor("#2DCCD3")
            canvas.drawRect(it.rect,paint)
        }

        canvas.restore()
    }

//    override fun onTouchEvent(event: MotionEvent): Boolean {
//        return onTouchEventSelf(event)
//    }
    private var lastX:Int = 0
    private var lastY:Int = 0
    fun onTouchEventSelf(event: MotionEvent): Boolean {
        super.onTouchEvent(event)
        when(event.action){
            MotionEvent.ACTION_DOWN ->{
                val x = event.x.toInt()
                val y = event.y.toInt()
                if(pointsRect.lockPosition(x,y)){
                    lastX = x;
                    lastY = y;
                }
            }
            MotionEvent.ACTION_MOVE ->{
                val x = event.x.toInt()
                val y = event.y.toInt()
                if(pointsRect.tryTrans(x-lastX,y-lastY)){
                    postInvalidate()
                    lastX = x;
                    lastY = y;
                }
            }
            MotionEvent.ACTION_UP ->{
                pointsRect.unlockPosition()
            }

        }
        return true
    }

    fun lock16_9(lock:Boolean){
        pointsRect.lock16_9 = lock
    }

    val bounds get() = pointsRect.bounds

    companion object {
        private const val TAG = "ImageCrop"
    }


}